package cn.com.digitalnet.ad.service;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springside.modules.web.Servlets;

import cn.com.digitalnet.ad.util.ClassUtil;
import cn.com.digitalnet.ad.util.LogUtil;
import cn.com.digitalnet.ad.util.MybatisUtil;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class CodeService {
	@Autowired private ClassUtil cu;
	@Autowired private LogUtil lu;
	@Autowired private MybatisUtil bt;
	private static final String DEFAULT_ENCODING = "utf-8";
	
	//request 为null时 是由表生成代码
	public void code(HttpServletRequest request, Template template, Configuration configuration, Map<String, Object> context, Model model) throws Exception{
//		List<String> fields = Lists.newArrayList();
		List<Map<String,String>> props= Lists.newArrayList();
		List<Map<String,String>> javaProps= Lists.newArrayList();
		List<Map<String,String>> fields= Lists.newArrayList();
		
		
		String clsName=(String) context.get("className");
		String className=formatClassName(clsName,"up");
		context.put("className", className);
		
		String elName=formatClassName(className, "low");
		//返回的list
		context.put("list",elName+"s");
		context.put("english",elName);
		Map<String,String> javaParam = Maps.newHashMap();
		Map<String,String> xmlProps = Maps.newHashMap();
		
		if(request!=null){
			Map<String, Object> fieldNames = Servlets.getParametersStartingWith(request, "fieldName");
			for(Map.Entry<String, Object> entry : fieldNames.entrySet()){
			String fieldName = (String)entry.getValue();
			String fieldType = request.getParameter("fieldType" + entry.getKey());
			Map<String,String> param = Maps.newHashMap();
			param.put(lu.propertyToField(fieldName), fieldType);
			javaParam.put(fieldName, cu.getJavaType(fieldType));
			props.add(param);
			xmlProps.put(lu.propertyToField(fieldName), fieldName);
			
			}
			fields.add(xmlProps);
			javaProps.add(javaParam);
			
		}else{
			cu.Table2Prop((String) context.get("tableName"), bt, javaParam,fields);
			javaProps.add(javaParam);
		}
		
		context.put("fields", fields);
		context.put("props", props);
		context.put("javaProps", javaProps);
		
		//生成web层
		template = configuration.getTemplate("springmvc_web.ftl", DEFAULT_ENCODING);
		String web = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_web\\"+className+"Controller.java"), web);
		
		//生成mybatis.xml
		template = configuration.getTemplate("mybatis_xml.ftl", DEFAULT_ENCODING);
		String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_mybatis_xml\\"+className+"Mapper.xml"), result);
		

		//生成service层
		template = configuration.getTemplate("service.ftl", DEFAULT_ENCODING);
		String dao = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_service\\"+className+"Service.java"), dao);
		
		//生成mao
		template = configuration.getTemplate("mao.ftl", DEFAULT_ENCODING);
		String mao = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_mao\\"+className+"Mao.java"), mao);
		
		//生成bean
		template = configuration.getTemplate("entity.ftl", DEFAULT_ENCODING);
		String entity = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_entity\\"+className+".java"), entity);
		
		//生成sql
		//size=0时 是由表创建 不用生成sql语句
		if(props.size()!=0){
			template = configuration.getTemplate("sql.ftl", DEFAULT_ENCODING);
			String sql = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
			FileUtils.writeStringToFile(new File("d:\\3r_sql\\"+className+".sql"), sql);
		}
		
		//生成list页面
		template = configuration.getTemplate("jspList.ftl", DEFAULT_ENCODING);
		String jspList = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_jsp\\"+context.get("english")+"List.jsp"), jspList);
		
		//生成jsp form页面
		template = configuration.getTemplate("jspForm.ftl", DEFAULT_ENCODING);
		String jspForm = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
		FileUtils.writeStringToFile(new File("d:\\3r_jsp\\"+context.get("english")+"Form.jsp"), jspForm);
		model.addAttribute("result", "生成成功，请去\n  d:\\3r_mybatis_xml\n d:\\3r_web\n d:\\3r_service\n d:\\3r_entity\n d:\\3r_sql\n d:\\3r_jsp   查看适用！");
	}
	
	/***********
	 * 转换第一个字母为大写or小写 strategy等于up是大写， stragegy等于low是小写
	 * @param came 转换的字符串， stragegy 转换的策略
	 * @return
	 */
	private String formatClassName(String came,String strategy) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		String f="";
		if(strategy.equalsIgnoreCase("up")){
			f = StringUtils.upperCase(CharUtils.toString(came.charAt(0)));
		}else if(strategy.equalsIgnoreCase("low")){
			f = StringUtils.lowerCase(CharUtils.toString(came.charAt(0)));
		}
		sb.append(f).append(came.substring(1));
		return sb.toString();
	}



}
